﻿package com.wandian.view.components.ui
{
	import com.wandian.ApplicationFacade;
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.data.GameData;
	import com.wandian.model.vo.RoomVo;

	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;

	/**
	* @author long
	* @QQ 108232706
	* @version 1.0
	*/
	public class RoomInfo extends EventDispatcher
	{
		public var roomVo:RoomVo;
		public var isDown:Boolean=false;
		public var nNum:int;
		private var _global:GameData;
		private var _arrSex:Array;
		private var mcInfo:MovieClip;
		private const UP1:int=1;
		private const UP2:int=2;
		private const OVER:int=3;
		private const DOWN:int=4;

//		public static const onRoomInfoChange:Event=new Event("onRoomInfoChange");

		public function RoomInfo(roomMc:MovieClip, roomV:RoomVo, _curNum:int):void
		{
			this._global=GameData.getInstance();
			this.roomVo=roomV;
			this.nNum=_curNum;
			this.mcInfo=roomMc;
//			trace("Num " + this.nNum + " Roomid " + this.roomVo.id);
			if (_curNum % 2 == 0)
			{
				roomMc.gotoAndStop(this.UP1);
			}
			else
			{
				roomMc.gotoAndStop(this.UP2);
			}
			this.mcInfo.buttonMode=true;
			this.mcInfo.mouseChildren=false;
			roomMc.close_mc.visible=false;
			roomMc.doubleClickEnabled=true;
			roomMc.addEventListener(MouseEvent.ROLL_OUT, this.outHandler);
			roomMc.addEventListener(MouseEvent.ROLL_OVER, this.overHandler);
			roomMc.addEventListener(MouseEvent.DOUBLE_CLICK, this.onDoubleHandler);
			roomMc.addEventListener(MouseEvent.CLICK, this.onHandler);
			this.setInfo();
//			this.addEventListener(onRoomInfoChange.type,onRoomInfoChangeHandler);
			return;
		}

		protected function onRoomInfoChangeHandler(event:Event):void
		{
			trace("房间属性更改");
			setInfo();
		}

		public function get num():int
		{
			return this.nNum;
		}

		public function onHandler(event:MouseEvent=null):void
		{
			this.isDown=true;
			this.mcInfo.gotoAndStop(this.DOWN);
			this.dispatchEvent(new Event(Event.CHANGE));
			return;
		}

		public function init():void
		{
			this.isDown=false;
			this.outHandler();
			return;
		}

		private function onDoubleHandler(event:MouseEvent):void
		{
			ApplicationFacade.getInstance().sendNotification(CLZNoticeName.ENTER_GAME_ROOM_REQ, this.roomVo);
			return;
		}

		private function outHandler(event:MouseEvent=null):void
		{
			if (!this.isDown)
			{
				if (this.num % 2 == 0)
				{
					this.mcInfo.gotoAndStop(this.UP1);
				}
				else
				{
					this.mcInfo.gotoAndStop(this.UP2);
				}
			}
			return;
		}

		private function overHandler(event:MouseEvent):void
		{
			if (!this.isDown)
			{
				this.mcInfo.gotoAndStop(this.OVER);
			}
			return;
		}

		public function setInfo():void
		{
			this.mcInfo.txt_name.cacheAsBitmap=true;
			this.mcInfo.txt_name.text=this.roomVo.name; //this.roomVo.roomCode=getRoomCode(this.roomVo.id);
			this.mcInfo.txt_id.text=this.roomVo.roomCode=getRoomCode(this.roomVo.id); //this.roomVo.id.toString();
			; //this.roomVo.id.toString();
			this.mcInfo.txt_type.text=GameData.tip["GOLD"]; //this._global.judgeMoneyType(this.roomVo.type) == 0 ? (GameData.tip["MONEY"]) : (GameData.tip["GOLD"]);
			this.mcInfo.txt_blind.text=this._global.counstK(this.roomVo.minBlind) + " / " + this._global.counstK(this.roomVo.minBlind * 2);
			this.mcInfo.txt_minStake.text=this._global.counstK(this.roomVo.minBlind);
			this.mcInfo.txt_maxStake.text=this._global.counstK(this.roomVo.maxStake);
			setPlayerNum();
			if (this.roomVo.close > 0)
			{
				this.mcInfo.close_mc.visible=true;
			}
			else
			{
				this.mcInfo.close_mc.visible=false;
			}
			return;
		}

		public function setPlayerNum():void
		{
			this.mcInfo.txt_playerNum.text=this.roomVo.playerNum + " / " + this.roomVo.seat;
			var frame:int=int(this.roomVo.playerNum / this.roomVo.seat * 100);
			this.mcInfo.playerNum.gotoAndStop(frame);
		}

		private function getRoomCode(id:uint):String
		{
			if (GameData.enableRoomCode != 1)
				return id.toString();
			var num:int=(((id - 1) / GameData.floorCount) + 1);
			var roomCode:int=num * 100 + (id - GameData.floorCount * (num - 1));
//			trace(id + " " + roomCode);
			return roomCode.toString();
		}


	}
}
